package com.shjy.signdemo;

import android.content.Context;
import android.content.DialogInterface;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

public class Main3Activity extends AppCompatActivity {

    private static final String TAG = "Main3Activity";

    private EditText editText;
    private GestureOverlayView gesture;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        mContext = this;

        final GestureLibrary mGestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/signDemo/gestures");
        mGestureLibrary.load();

        //获取手势编辑组件后，设置相关参数
        gesture = (GestureOverlayView) findViewById(R.id.gesture);
        gesture.setGestureColor(Color.GREEN);
        gesture.setGestureStrokeWidth(5);
        gesture.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {

            @Override
            public void onGesturePerformed(GestureOverlayView gestureOverlayView, final Gesture gesture) {

                //识别用户刚绘制的手势
                ArrayList<Prediction> predictions = mGestureLibrary.recognize(gesture);
                ArrayList<String> result = new ArrayList<String>();

                Log.d(TAG, "手势 count " + mGestureLibrary.getGestureEntries().size());

                //遍历所有找到的Prediction对象
                for (Prediction pred : predictions) {
                    Log.d(TAG, "手势 " + pred.name + ", 相似度为" + pred.score);
                    if (pred.score > 2.0) {
                        result.add("与手势【" + pred.name + "】相似度为" + pred.score);
                    }
                }
                if (result.size() == 0) {
                    result.add("无法找到匹配的手势！");
                }

                ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(mContext,
                        android.R.layout.simple_dropdown_item_1line, result.toArray());
                new AlertDialog.Builder(mContext).setAdapter(adapter, null).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).show();
            }
        });
    }

}
